package com.lapelife.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lapelife.backend.common.ErrorCode;
import com.lapelife.backend.common.ResponseResult;
import com.lapelife.backend.model.domain.Post;
import com.lapelife.backend.model.request.PostRequest;
import com.lapelife.backend.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "文章查询")
@RestController
@RequestMapping("post/query")
public class PostQueryController {
    private PostService postService;

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @ApiOperation("查询符合所有条件的文章")
    @PostMapping("/and")
    public ResponseResult<Page<Post>> queryPost(@RequestBody PostRequest postRequest) {
        return postService.queryPost(postRequest);
    }

    @ApiOperation("查询符合任意条件的文章")
    @PostMapping("/or")
    public ResponseResult<Page<Post>> queryPostByKey(@RequestBody PostRequest postRequest) {
        return postService.queryPostByKey(postRequest);
    }

    @ApiOperation("通过id查询文章")
    @GetMapping("/id/{id}")
    public ResponseResult<Post> queryPostById(@PathVariable Long id) {
        if (id == null) {
            return ResponseResult.fail(ErrorCode.PARAMS_ERROR);
        }
        return ResponseResult.suc(postService.getById(id));
    }


}

